#!/usr/bin/python2.4


#we get two parameters repo-path and rev no

import sys, os
import commands
import logging

logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(levelname)s %(message)s',
                    filename='/home/example/hooks.log',
                    filemode='a')

sys.path += ["/home/amitu/python_modules"]
os.environ["PYTHONPATH"] = "/home/amitu/python_modules"
os.environ["PATH"] = "/home/amitu/bin:/usr/bin"
os.environ["HOME"] = "/home/example/repo"

def main():
    # get the parameters
    try: _, repo, rev = sys.argv
    except Exception, e: 
        logging.exception("Failure while unpacking arguments.")
        print >> sys.stderr, "Not enough arguments to script. "
        sys.exit(1)
    # to get log message, we have to call:
    #       $ svnlook log -t rev repo
    # and use the output
    logging.info("svnlook log -r %s %s" % (rev, repo))
    stat, log = commands.getstatusoutput("svnlook log -r %s %s" % (rev, repo))
    if stat: 
        logging.error("svnlook exited with non zero exit code: %s" % log)
        print >> sys.stderr, "svnlook exited with non zero exit code:", log
        sys.exit(1)
    # we got the log successfully, no implement all the checks:
    # if there is some log
    if not log.strip():
        logging.warn("Empty log message, please enter BUG:bug no followed by log msg.")
        sys.exit(1)
    log = log.lstrip()
    parts = log.split(" ", 1)
    if len(parts) != 2 or parts[1].strip() == "": 
        logging.warn("Log message should contain BUG no and some message.")
        sys.exit(1)
    word = parts[0].strip().lower()
    if not word.startswith("bug:"):
        logging.warn("Log message does not begin with BUG:")
        sys.exit(1)
    bug_no = word[4:]
    try: bug_no= int(bug_no)
    except ValueError:
        logging.warn("BUG no supplied is not integer.")
        sys.exit(1)
    stat, author = commands.getstatusoutput("svnlook author -r %s %s" % (rev, repo))
    if stat:
        logging.warn("Some error getting output from svnlook.")
        sys.exit(1)
    # we have author, bug_no set. we need to post a comment to the bug with appropriate message
    changesheet = "http://svn.example.com/viewvc/bin/cgi/viewvc.cgi?view=rev&revision=%s" % rev
    comment = "%s mode the following changes. Log:\n%s\n\nChangesheet: %s\n" % (author, log, changesheet)
    stat, changed = commands.getstatusoutput("svnlook changed -r %s %s" % (rev, repo))
    if stat: 
        logging.warn("svnlook exited with non zero exit code:", changed)
        sys.exit(1)
    comment += "Modifications: \n%s" % changed
    comment = comment.replace('"', r'\"')
    #comment = comment.replace('\n', '\\n')
    stat, changed = commands.getstatusoutput('/home/amitu/bin/bugz -b http://bugs.example.com/ -u checkin-bot@example.com -p "password" modify %d -c "%s"' % (bug_no, comment))
    if stat: 
        logging.warn("bugz exited with non zero exit code: %s" % changed)
        sys.exit(1)
    
if __name__ == "__main__": main()

